﻿using System;
using System.Diagnostics;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System.IO;
using System.IO.IsolatedStorage;
using System.Xml.Linq;
using Microsoft.Xna.Framework.Input;

namespace chemicall
{
    public class ScreenManager : DrawableGameComponent
    {
        SpriteBatch spriteBatch;
        SpriteFont font;
        bool isInitialized;
        bool traceEnabled;
        public GameScreen CurrentScreen, LastScreen;
        public ContentManager content;

        public SpriteBatch SpriteBatch
        {
            get { return spriteBatch; }
        }

        public SpriteFont Font
        {
            get { return font; }
        }

        public bool TraceEnabled
        {
            get { return traceEnabled; }
            set { traceEnabled = value; }
        }

        public ScreenManager(Game game)
            : base(game)
        {
            //TODO
        }

        public override void Initialize()
        {
            base.Initialize();

            isInitialized = true;
        }

        protected override void LoadContent()
        {
            content = Game.Content;

            spriteBatch = new SpriteBatch(GraphicsDevice);
            font = content.Load<SpriteFont>("classicPolice");
            if (CurrentScreen != null)
                CurrentScreen.Activate(false);
        }

        public void AddScreen(GameScreen screenP, PlayerIndex? controllingPlayer)
        {
            if (CurrentScreen != null)
                LastScreen = CurrentScreen; 
            CurrentScreen = screenP;
            CurrentScreen.ControllingPlayer = controllingPlayer;
            CurrentScreen.ScreenManager = this;
            CurrentScreen.IsExiting = false;

            if (isInitialized)
            {
                LastScreen.Activate(true);
                CurrentScreen.Activate(false);
            }
        }

        public override void Update(GameTime gameTime)
        {
            bool otherScreenHasFocus = !Game.IsActive;
            bool coveredByOtherScreen = false;
            int cmpt = 0;

            if (CurrentScreen != null)
            {
                CurrentScreen.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);

                if (CurrentScreen.ScreenState == ScreenState.TransitionOn ||
                    CurrentScreen.ScreenState == ScreenState.Active)
                {
                    if (!otherScreenHasFocus)
                    {
                        cmpt++;
                        if (cmpt == 1)
                            CurrentScreen.HandleInput(gameTime, Keyboard.GetState());
                        otherScreenHasFocus = true;
                    }

                    if (!CurrentScreen.IsPopup)
                        coveredByOtherScreen = true;
                }
            }
        }

        //On dessine tous les screen (ceux qui sont chargés)
        public override void Draw(GameTime gameTime)
        {
            if (CurrentScreen != null && CurrentScreen.ScreenState == ScreenState.Active)
                CurrentScreen.Draw(gameTime);
        }
    }
}
